/**
 * 
 */
package com.asdf.db;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.asdf.db.DataBase.DataSource;
import com.asdf.external.DBUsageDetails;
import com.asdf.external.DBUserDetails;
import com.asdf.external.IDatabaseQuery;

/**
 * @author ilia
 * FIXME quick and dirty implementation, tightly coupled to DataBase class
 */
public class DatabaseQuery implements IDatabaseQuery
{
	private List<DBUserDetails>		users;
	private List<DBUsageDetails>	stats;

	@SuppressWarnings("unchecked")
	public DatabaseQuery()
	{
		DataBase db = DataBase.getInstance();
		users = (List<DBUserDetails>)	db.getDataSource(DataSource.UserList);
		stats = (List<DBUsageDetails>)	db.getDataSource(DataSource.StatList);
	}

	@SuppressWarnings("deprecation")
	private static boolean isSameDay(Date date1, Date date2) {
		if (date1==null || date2==null)
			return false;
		return  date1.getYear()  == date2.getYear() &&
				date1.getMonth() == date2.getMonth() &&
				date1.getDate()  == date2.getDate();
	}
	
	/** Given the airport id and date, provide all network usages in
	 * that airport on that date
	 * @see com.asdf.external.IDatabaseQuery#getDailyUsageByAirport(java.lang.String, java.util.Date)
	 */
	@Override
	public LinkedList<DBUsageDetails> getDailyUsageByAirport( String id, Date date)
	{
		LinkedList<DBUsageDetails> res = new LinkedList<DBUsageDetails>();
		if (id==null || date==null)
			return res;
		for(DBUsageDetails s : stats) {
			if ( s.getAirportId().equalsIgnoreCase(id) && isSameDay(date, s.getDate()) ) 
				res.add(s);
		}
		return res;
	}

	/** Given the user id and date, provide all network usages by the
	 * user on that date
	 * @see com.asdf.external.IDatabaseQuery#getDailyUsageByUser(java.lang.String, java.util.Date)
	 */
	@Override
	public LinkedList<DBUsageDetails> getDailyUsageByUser( String id, Date date)
	{
		LinkedList<DBUsageDetails> res = new LinkedList<DBUsageDetails>();
		if (id==null || date==null)
			return res;
		for(DBUsageDetails s : stats) {
			if (s.getUserId().equalsIgnoreCase(id) && isSameDay(date, s.getDate()) )
				res.add(s);
		}
		return res;
	}

	/** Provides personal details of the subscribed user given
	 * his id. If there is no subscribed user with a given id,
	 * returns null
	 * @see com.asdf.external.IDatabaseQuery#getUser(java.lang.String)
	 */
	@Override
	public DBUserDetails getUser( String id)
	{
		if (id==null)
			return null;
		for (DBUserDetails u : users) {
			if (u.getId().equalsIgnoreCase(id))
				return u;
		}
		return null;
	}

	/** Given the id of the user, check if the user is subscribed
	 * @see com.asdf.external.IDatabaseQuery#isSubscribedUser(java.lang.String)
	 */
	@Override
	public boolean isSubscribedUser( String id)
	{
		return getUser(id) != null;
	}

	/** Given user id and password, checks if the password is valid
	 * @see com.asdf.external.IDatabaseQuery#isValidPassword(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean isValidPassword( String id, String password)
	{
		if (id==null || password==null)
			return false;
		DBUserDetails u = getUser(id);
		if (u == null)
			return false;
		return u.getPassword().equals(password);
	}
}
